package com.bw.paper.mvp.model;

import com.bw.paper.entity.ResponseUserEntity;
import com.bw.paper.entity.UploadEntity;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.model.BaseModel;
import com.bw.paper.mvp.contact.LoginContract;
import com.bw.paper.Api;
import com.bw.paper.network.ChangeFunction;
import com.bw.paper.network.HttpFactory;
import com.bw.paper.network.HttpType;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class LoginModel extends BaseModel implements LoginContract.LoginModel {

    @Inject
    public LoginModel() {
    }


    @Override
    public Observable<BaseEntity> request(Map<String, Object> map) {
        //区分当前方法执行哪个接口的请求
        int code = (int) map.get("code");

        List<MultipartBody.Part> list = (List<MultipartBody.Part>) map.get("list");
        map.remove("list");
        map.remove("code");
        if (code == HttpCode.LOGINCODE){
            RequestBody body = createBody(map);
            Observable<ResponseUserEntity> ob = HttpFactory.
                    getInstance().factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class).requestLogin(body);
            return ob.map(new ChangeFunction<ResponseUserEntity>());
        }else
            if (code == HttpCode.REGISTCODE){
            RequestBody body = createBody(map);
            Observable<ResponseUserEntity> ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit().create(Api.class).requestRegister(body);
            return  ob.map(new ChangeFunction<ResponseUserEntity>());
        }else if (code == HttpCode.UPLOADIMGCODE){
                Observable<UploadEntity> ob = HttpFactory.getInstance().factory(HttpType.UPLOADTYPE)
                        .getRetrofit().create(Api.class)
                        .requestUpload(list);
                return ob.map(new ChangeFunction<UploadEntity>());
            } else if (code == HttpCode.UPDATEUSERCODE){
                RequestBody body = createBody(map);
                Observable<BaseEntity> ob = HttpFactory.getInstance()
                        .factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class).requestUpdateUser(body);
                return  ob.map(new ChangeFunction<BaseEntity>());
            }



        return null;
    }
}
